﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LocalDataBase
{
    class TaskHelper
    {
        const string ConnectionString = "isostore:/TaskDB.sdf";

        public static void CreateDatabase()
        {
            using (TaskDB context = new TaskDB(ConnectionString))
            {
                if (!context.DatabaseExists())
                {
                    context.CreateDatabase();
                }
            }
        }
        public static void DeleteDatabase()
        {
            using (TaskDB context = new TaskDB(ConnectionString))
            {
                if (context.DatabaseExists())
                {
                    context.DeleteDatabase();
                }
            }
        }
        public static bool AddTask(Tasks NewTask)
        {
            bool bSuccess = false;
            using (TaskDB context = new TaskDB(ConnectionString))
            {
                if (context.DatabaseExists())
                {
                    context.Tasks.InsertOnSubmit(NewTask);
                    context.SubmitChanges();
                    bSuccess = true;
                }
            }
            return bSuccess;
        }
        public static void DeleteTask(int TaskID)
        {
            using (TaskDB context = new TaskDB(ConnectionString))
            {
                if (context.DatabaseExists())
                {
                    Tasks TaskToDelete = (from t in context.Tasks
                                                              where t.ID == TaskID
                                                              select t).Single();
                    context.Tasks.DeleteOnSubmit(TaskToDelete);
                    context.SubmitChanges();
                }
            }
        }
        public static IList<Tasks> GetTasks()
        {
            IList<Tasks> TaskList = null;
            using (TaskDB context = new TaskDB(ConnectionString))
            {
                if (context.DatabaseExists())
                {
                    TaskList = context.Tasks.ToList();

                    //TaskList = (from t in context.Tasks select t).ToList();
                }
            }
            return TaskList;
        }

    }
}
